package com.basic.auth.service.impl;

import com.basic.auth.dao.mapper.*;
import com.basic.auth.dao.model.*;
import com.basic.auth.domain.AuthFuncBo;
import com.basic.auth.domain.AuthRoleFuncBo;
import com.basic.auth.domain.AuthSpecFuncBo;
import com.basic.auth.domain.AuthUserFuncBo;
import com.basic.auth.service.AuthFuncService;
import com.basic.auth.service.AuthRoleFuncService;
import com.basic.auth.service.AuthUserFuncService;
import com.basic.common.bean.BeanCopierDsUtil;
import com.basic.common.bean.BeanCopyDsUtil;
import com.basic.common.constant.CommonDsConstant;
import com.basic.common.result.WebResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 功能管理
 *
 * @author
 * @version 1.0
 * @Date 8/14 9:46
 */
@Service
public class AuthFuncServiceImpl implements AuthFuncService {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private AuthFuncMapper authFuncMapper;
    @Autowired
    private AuthUserFuncService authUserFuncService;
    @Autowired
    private AuthRoleFuncService authRoleFuncService;
    @Resource
    AuthUserFuncMapper authUserFuncMapper;
    @Resource
    private AuthRoleMemberMapper authRoleMemberMapper;
    @Resource
    private AuthRoleFuncMapper authRoleFuncMapper;
    @Autowired
    private AuthFuncExMapper authFuncExMapper;


    @Override
    public List<AuthFunc> selFuncListByParam(AuthFuncBo authFuncBo) {
//        if (authFuncBo == null) {
//            throw new ValidationException(Errors.SYSTEM_REQUEST_PARAM_ERROR);
//        }
        AuthFuncExample example = new AuthFuncExample();
        AuthFuncExample.Criteria criteria = example.createCriteria();
        criteria.andIsEnabledEqualTo(CommonDsConstant.IS_ENABLED_VAL);
        //功能编号集合
        if (CollectionUtils.isNotEmpty(authFuncBo.getFuncIdList())) {
            criteria.andIdIn(authFuncBo.getFuncIdList());
        }
//        if(Strings.isNotBlank(authFuncBo.getCode())){
//            criteria.andCodeEqualTo(authFuncBo.getCode());
//        }
        //所属系统
        if (authFuncBo.getSystemId() != null) {
            criteria.andSystemIdEqualTo(authFuncBo.getSystemId());
        }
        if (Strings.isNotBlank(authFuncBo.getAxisLike())) {
            criteria.andAxisLike(authFuncBo.getAxisLike() + "%");
        }
        //父功能编号
        if (authFuncBo.getParentFuncId() != null) {
            criteria.andParentFuncIdEqualTo(authFuncBo.getParentFuncId());
        }
        if (CollectionUtils.isNotEmpty(authFuncBo.getParentFuncIdList())) {
            criteria.andParentFuncIdIn(authFuncBo.getParentFuncIdList());
        }
        //子系统
        if (authFuncBo.getSubSystemId() != null) {
            criteria.andSubSystemIdEqualTo(authFuncBo.getSubSystemId());
        }
        //类型
        if (Strings.isNotBlank(authFuncBo.getType())) {
            criteria.andTypeEqualTo(authFuncBo.getType());
        }
        //类型集合
        if (CollectionUtils.isNotEmpty(authFuncBo.getTypeList())) {
            criteria.andTypeIn(authFuncBo.getTypeList());
        }
        //用户身份类型
        if (Strings.isNotBlank(authFuncBo.getUserTypeLike())) {
            criteria.andUserTypeMatchLike("%" + authFuncBo.getUserTypeLike() + "%");
        }
        //功能模块编码集合
        if (CollectionUtils.isNotEmpty(authFuncBo.getCodeList())) {
            criteria.andCodeIn(authFuncBo.getCodeList());
        }
        //功能模块排除编码集合
        if (CollectionUtils.isNotEmpty(authFuncBo.getCodeNotInList())) {
            criteria.andCodeNotIn(authFuncBo.getCodeNotInList());
        }
        // 是否后台
        if (authFuncBo.getIsBack() != null) {
            criteria.andIsBackEqualTo(authFuncBo.getIsBack());
        }
        // 管理类型
        if (Strings.isNotBlank(authFuncBo.getManageType())) {
            criteria.andManageTypeEqualTo(authFuncBo.getManageType());
        }
        if (Strings.isNotBlank(authFuncBo.getSubSystemIds())) {
            String[] subSystemIds = authFuncBo.getSubSystemIds().split(CommonDsConstant.DELIMITER_SG_ZY);
            List<Short> subSystemIdList = new ArrayList<Short>();
            for (String subSystemId : subSystemIds) {
                subSystemIdList.add(Short.parseShort(subSystemId));
            }
            criteria.andSubSystemIdIn(subSystemIdList);
        }
        example.setOrderByClause("sort asc");
        List<AuthFunc> authFuncList = authFuncMapper.selectByExample(example);
        return authFuncList;
    }

    /**
     * 查询子孙功能集合（不包含自身） 是否被选中
     *
     * @param authFuncBo
     * @return java.util.List<com.nxin.middle.basic.auth.dao.model.AuthFunc>
     * @author
     * @date 11:06 8/17
     **/
    @Override
    public List<AuthFuncBo> selSubFuncList(AuthFuncBo authFuncBo) {
//        if (authFuncBo == null
//                || authFuncBo.getSystemId() == null
//                || authFuncBo.getParentFuncId() == null) {
//            throw new ValidationException(Errors.SYSTEM_REQUEST_PARAM_ERROR);
//        }
        List<Long> funcIdList = null;
        if (authFuncBo.getAuthBoId() != null && authFuncBo.getBoId() != null) {
            // 查询用户被授予的功能
            funcIdList = selUserFuncIdList(authFuncBo);
        } else if (Strings.isNotBlank(authFuncBo.getRoleCodes())) {
            // 查询所选角色的默认功能
            funcIdList = selRoleFuncIdList(authFuncBo);
        }
        //作为父级节点用，便于递归查询lipengpeng
        authFuncBo.setId(authFuncBo.getParentFuncId());
        List<AuthFuncBo> authFuncBoList = selSubFuncListDiGui(authFuncBo, funcIdList);
        return authFuncBoList;
    }

    /**
     * 查询用户被授予的功能
     *
     * @param authFuncBo
     * @return java.util.List<java.lang.Long>
     * @author
     * @date 11:09 2019/4/25
     */
    public List<Long> selUserFuncIdList(AuthFuncBo authFuncBo) {
        // 查询用户被授予的功能
        AuthUserFuncBo userFuncBo = new AuthUserFuncBo();
        userFuncBo.setBoId(authFuncBo.getBoId());
        userFuncBo.setAuthBoId(authFuncBo.getAuthBoId());
        userFuncBo.setSystemId(authFuncBo.getSystemId());
        userFuncBo.setSubSystemId(authFuncBo.getSubSystemId());
        List<AuthUserFunc> userFuncBoList = authUserFuncService.selUserFuncListByParam(userFuncBo);
        if (CollectionUtils.isEmpty(userFuncBoList)) {
            return null;
        }
        List<Long> funcIdList = new ArrayList<Long>();
        for (AuthUserFunc userFuncBoEntity : userFuncBoList) {
            funcIdList.add(userFuncBoEntity.getFuncId());
        }
        return funcIdList;
    }

    /**
     * 查询所选角色的默认功能
     *
     * @param authFuncBo
     * @return java.util.List<java.lang.Long>
     * @author
     * @date 11:09 2019/4/25
     */
    public List<Long> selRoleFuncIdList(AuthFuncBo authFuncBo) {
        // 查询所选角色的默认功能
        String[] roleCodes = authFuncBo.getRoleCodes().split(CommonDsConstant.DELIMITER_SG_ZY);
        AuthRoleFuncBo roleFunc = new AuthRoleFuncBo();
        roleFunc.setSystemId(authFuncBo.getSystemId());
        roleFunc.setSubSystemId(authFuncBo.getSubSystemId());
        roleFunc.setRoleCodeList(Arrays.asList(roleCodes));
        List<AuthRoleFunc> roleFuncList = authRoleFuncService.selListByParam(roleFunc);
        if (CollectionUtils.isEmpty(roleFuncList)) {
            return null;
        }
        List<Long> funcIdList = new ArrayList<Long>();
        for (AuthRoleFunc entity : roleFuncList) {
            funcIdList.add(entity.getFuncId());
        }
        return funcIdList;
    }

    /**
     * 递归查询功能子孙节点(不包含自身)
     *
     * @param authFuncBo
     * @param funcIdList
     * @return java.util.List<com.nxin.middle.basic.auth.domain.AuthFuncBo>
     * @author
     * @date 11:05
     */
    public List<AuthFuncBo> selSubFuncListDiGui(AuthFuncBo authFuncBo, List<Long> funcIdList) {
        AuthFuncBo param = new AuthFuncBo();
        param.setSystemId(authFuncBo.getSystemId());
        param.setSubSystemId(authFuncBo.getSubSystemId());
        param.setParentFuncId(authFuncBo.getId());
        param.setUserType(authFuncBo.getUserType());
        List<AuthFunc> authFuncList = selFuncListByParam(param);
        if (CollectionUtils.isEmpty(authFuncList)) {
            return null;
        }
        List<AuthFuncBo> authFuncBoList = BeanCopierDsUtil.copyPropertiesOfList(authFuncList, AuthFuncBo.class);

        for (AuthFuncBo authFuncBoEntity : authFuncBoList) {
            // 判断是否被选中
            authFuncBoEntity.setIsChecked(CommonDsConstant.DEFAULT_INT_NO);
            if (CollectionUtils.isNotEmpty(funcIdList) && funcIdList.contains(authFuncBoEntity.getId())) {
                authFuncBoEntity.setIsChecked(CommonDsConstant.DEFAULT_INT_YES);
            }
            authFuncBoEntity.setSubFuncList(selSubFuncListDiGui(authFuncBoEntity, funcIdList));
        }
        return authFuncBoList;
    }

    @Override
    public WebResult selSubFuncPageList(AuthFuncBo authFuncBo) {
        PageHelper.startPage(authFuncBo.getPageNum(), authFuncBo.getPageSize());
//        if (authFuncBo == null || authFuncBo.getSystemId() == null) {
//            throw new ValidationException(Errors.SYSTEM_REQUEST_PARAM_ERROR);
//        }
        AuthFuncExample example = new AuthFuncExample();
        AuthFuncExample.Criteria criteria = example.createCriteria();
        criteria.andIsEnabledEqualTo(CommonDsConstant.IS_ENABLED_VAL);
        criteria.andSystemIdEqualTo(authFuncBo.getSystemId());
        if (authFuncBo.getRank() != null) {
            criteria.andRankEqualTo(authFuncBo.getRank());
        }
        if (authFuncBo.getSubSystemId() != null) {
            criteria.andSubSystemIdEqualTo(authFuncBo.getSubSystemId());
        }
        if (authFuncBo.getParentFuncId() != null) {
            criteria.andParentFuncIdEqualTo(authFuncBo.getParentFuncId());
        }
        if (authFuncBo.getIsBack() != null) {
            criteria.andIsBackEqualTo(authFuncBo.getIsBack());
        }
        // 管理类型
        if (Strings.isNotBlank(authFuncBo.getManageType())) {
            criteria.andManageTypeEqualTo(authFuncBo.getManageType());
        }
        if (Strings.isNotBlank(authFuncBo.getSubSystemIds())) {
            String[] subSystemIds = authFuncBo.getSubSystemIds().split(CommonDsConstant.DELIMITER_SG_ZY);
            List<Short> subSystemIdList = new ArrayList<Short>();
            for (String subSystemId : subSystemIds) {
                subSystemIdList.add(Short.parseShort(subSystemId));
            }
            criteria.andSubSystemIdIn(subSystemIdList);
        }
        // example.setOrderByClause("sort asc");
        example.setOrderByClause("create_time desc");
        List<AuthFunc> authFuncList = authFuncMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(authFuncList);
        if (CollectionUtils.isEmpty(authFuncList)) {
            return WebResult.success(pageInfo);
        }
        List<AuthFuncBo> authFuncBoList = new ArrayList<AuthFuncBo>();
        AuthFuncBo entity = null;
        for (AuthFunc func : authFuncList) {
            entity = new AuthFuncBo();
            BeanCopyDsUtil.copyProperties(func, entity);
            entity.setIsChecked(CommonDsConstant.DEFAULT_INT_NO);
            if (Strings.isNotBlank(authFuncBo.getBusType()) && Strings.isNotBlank(authFuncBo.getBusId()) && authFuncBo.getBoId() != null) {
                // 判断当前用户是否有此功能
                AuthUserFuncBo userFuncBo = new AuthUserFuncBo();
                /* boid funcId systemId subSystemId busType busId */
                userFuncBo.setBoId(authFuncBo.getBoId());
                userFuncBo.setBusId(authFuncBo.getBusId());
                userFuncBo.setBusType(authFuncBo.getBusType());
                userFuncBo.setSystemId(func.getSystemId());
                userFuncBo.setSubSystemId(func.getSubSystemId());
                userFuncBo.setFuncId(func.getId());
                List<AuthUserFunc> userFuncBoList = authUserFuncService.selUserFuncListByParam(userFuncBo);
                if (!CollectionUtils.isEmpty(userFuncBoList)) {
                    entity.setIsChecked(CommonDsConstant.DEFAULT_INT_YES);
                }
            }
            if (Strings.isNotBlank(authFuncBo.getRoleCodes())) {
                // 判断所选角色是否有此功能
                String[] roleCodes = authFuncBo.getRoleCodes().split(CommonDsConstant.DELIMITER_SG_ZY);
                for (String roleCode : roleCodes) {
                    AuthRoleFuncBo roleFunc = new AuthRoleFuncBo();
                    roleFunc.setFuncId(func.getId());
                    roleFunc.setSystemId(func.getSystemId());
                    roleFunc.setSubSystemId(func.getSubSystemId());
                    roleFunc.setRoleCode(roleCode);
                    List<AuthRoleFunc> roleFuncList = authRoleFuncService.selListByParam(roleFunc);
                    if (!CollectionUtils.isEmpty(roleFuncList)) {
                        entity.setIsChecked(CommonDsConstant.DEFAULT_INT_YES);
                    }
                }
            }
            AuthFuncBo param = new AuthFuncBo();
            param.setSystemId(authFuncBo.getSystemId());
            param.setSubSystemId(authFuncBo.getSubSystemId());
            param.setParentFuncId(func.getId());
            param.setBusId(authFuncBo.getBusId());
            param.setBusType(authFuncBo.getBusType());
            param.setBoId(authFuncBo.getBoId());
            param.setRoleCodes(authFuncBo.getRoleCodes());
            entity.setSubFuncList(selSubFuncList(param));
            authFuncBoList.add(entity);
        }
        pageInfo.setList(authFuncBoList);
        return WebResult.success(pageInfo);
    }

    /**
     * 根据id查询功能信息
     *
     * @param id
     * @return com.nxin.middle.basic.auth.dao.model.AuthFunc
     * @author
     * @date 18:31 8/14
     **/
    @Override
    public AuthFunc selAuthFuncById(Long id) {
//        if (id == null) {
//            throw new ValidationException(Errors.SYSTEM_REQUEST_PARAM_ERROR);
//        }
        AuthFunc authFunc = authFuncMapper.selectByPrimaryKey(id);
        return authFunc;
    }

    /**
     * 根据id修改功能信息
     *
     * @return int
     * @author
     * @date 18:28 8/14
     **/
    @Override
    public int updAuthFuncById(AuthFunc authFunc) {
        log.info("==========修改内容==========" + authFunc);
//        if (authFunc == null || authFunc.getId() == null
//                || Strings.isBlank(authFunc.getName())
//                || Strings.isBlank(authFunc.getType())) {
//            throw new ValidationException(Errors.SYSTEM_REQUEST_PARAM_ERROR);
//        }

        if (authFunc.getParentFuncId() != null) {
            if (authFunc.getParentFuncId() != 0L) {
                authFunc.setAxis("$" + authFunc.getParentFuncId() + "$" + authFunc.getId());
            } else {
                authFunc.setAxis("$" + authFunc.getId());
            }
        }

        int result = authFuncMapper.updateByPrimaryKeySelective(authFunc);
//        if (result <= 0) {
//            throw new ServerServiceException(Errors.SYSTEM_UPDATE_ERROR);
//        }
        return result;
    }

    /**
     * 根据主键删除
     *
     * @return int
     * @author
     * @date 18:25 8/14
     **/
    @Override
    public int delAuthFuncById(Long id) {
        log.info("==========删除id==========" + id);
//        if (id == null) {
//            throw new ValidationException(Errors.SYSTEM_REQUEST_PARAM_ERROR);
//        }
        AuthFunc authFunc = new AuthFunc();
        authFunc.setId(id);
        authFunc.setIsEnabled(CommonDsConstant.IS_NOT_ENABLED_VAL);
        int result = authFuncMapper.updateByPrimaryKeySelective(authFunc);
//        if (result <= 0) {
//            throw new ServerServiceException(Errors.SYSTEM_DELETE_ERROR);
//        }
        return result;
    }

    /**
     * 添加功能信息
     *
     * @param authFunc
     * @return void
     * @author
     * @date 17:42 8/14
     **/
    @Override
    public void addAuthFunc(AuthFunc authFunc) {

        log.info("==========添加内容==========" + authFunc);
//        if (authFunc == null
//                || Strings.isBlank(authFunc.getName())
//                || Strings.isBlank(authFunc.getType())) {
//            throw new ValidationException(Errors.SYSTEM_REQUEST_PARAM_ERROR);
//        }

        //设置默认创建时间
        authFunc.setCreateTime(new Date());
        //设置默认状态
        authFunc.setIsEnabled(CommonDsConstant.IS_ENABLED);
        //设置默认为末节点
        authFunc.setIsEnd((short) 1);
        Long parentFuncId = authFunc.getParentFuncId();
        //查询父节点
        AuthFunc func = selAuthFuncById(parentFuncId);
        //根据父节点级别设置现节点级别
        if (func == null) {
            authFunc.setRank(1);
        } else {
            switch (func.getRank()) {
                case 0:
                    authFunc.setRank(1);
                    break;
                case 1:
                    authFunc.setRank(2);
                    break;
                case 2:
                    authFunc.setRank(3);
                    break;
                default:
                    authFunc.setRank(1);
                    break;
//                throw new ValidationException(Errors.SYSTEM_INSERT_ERROR);

            }
        }

        //设置默认类型
        if (authFunc.getType() == null) {
            authFunc.setType("MENU");
        }
        //设置默认管理类型
        if (authFunc.getManageType() == null) {
            authFunc.setManageType("YY");
        }
        //设置默认后台管理
        if (authFunc.getIsBack() == null) {
            authFunc.setIsBack((short) 1);
        }
        authFuncExMapper.insertAuthFunc(authFunc);
        //设置默认顺序
        if (authFunc.getSort() == null) {
            authFunc.setSort(Math.toIntExact(authFunc.getId()));
        }
        //添加返回主键
        if (parentFuncId != 0L) {
            authFunc.setAxis("$" + parentFuncId + "$" + authFunc.getId());
        } else {
            authFunc.setAxis("$" + authFunc.getId());
        }

        authFuncMapper.updateByPrimaryKeySelective(authFunc);
        //设置父节点不是末节点
        authFunc.setIsEnd((short) 0);
        authFuncMapper.updateByPrimaryKeySelective(authFunc);
        //新增菜单角色功能管理
        AuthRoleFuncBo authRoleFunc = new AuthRoleFuncBo();
        //设置默认角色代码
        if (authRoleFunc.getRoleCode() == null) {
            authRoleFunc.setRoleCode("SUPER_DEFAULT");
        }
        //设置默认子系统
        if (authRoleFunc.getSubSystemId() != null) {
            authRoleFunc.setSubSystemId(authFunc.getSubSystemId());
        }
        //设置默认所属系统
        if (authRoleFunc.getSystemId() != null) {
            authRoleFunc.setSystemId(authFunc.getSystemId());
        }
        //设置功能ID
        authRoleFunc.setFuncId(authFunc.getId());
        authRoleFuncMapper.insertSelective(authRoleFunc);


//        if (result <= 0) {
//            throw new ServerServiceException(Errors.SYSTEM_INSERT_ERROR);
//        }


    }

    /**
     * 根据条件递归查询子孙功能集合，只有可用的
     *
     * @param authFuncBo
     * @return java.util.List<com.nxin.middle.basic.auth.domain.AuthFuncBo>
     * @author
     * @since 2019/4/19 10:51
     */
    @Override
    public List<AuthFuncBo> selSubFuncListByParam(AuthFuncBo authFuncBo) {
//        if (authFuncBo == null || authFuncBo.getSystemId() == null || (authFuncBo.getParentFuncId() == null && Strings.isBlank(authFuncBo.getParentFuncCode()))
//                || Strings.isBlank(authFuncBo.getUserTypeLike()) || authFuncBo.getSubSystemId() == null) {
//            throw new ValidationException(Errors.SYSTEM_REQUEST_PARAM_ERROR);
//        }
        log.info("根据条件查询子功能集合请求入参为：{}", authFuncBo.toString());
        if (authFuncBo.getParentFuncId() == null) {
            AuthFuncExample authFuncExample = new AuthFuncExample();
            AuthFuncExample.Criteria criteria = authFuncExample.createCriteria();
            criteria.andCodeEqualTo(authFuncBo.getParentFuncCode());
            criteria.andIsEnabledEqualTo(CommonDsConstant.IS_ENABLED_VAL);
            List<AuthFunc> funcList = authFuncMapper.selectByExample(authFuncExample);
            if (CollectionUtils.isEmpty(funcList)) {
                return null;
            }
            authFuncBo.setParentFuncIdList(funcList.stream().map(AuthFunc::getId).collect(Collectors.toList()));
        }
        List<AuthFuncBo> authFuncBoList = null;
        //判断是否是全部权限
        if (authFuncBo.getBoId().toString().equals(authFuncBo.getBusId())) {
            authFuncBoList = selSubList(authFuncBo);
            return authFuncBoList;
        }
        AuthUserFuncExample authUserFuncExample = new AuthUserFuncExample();
        AuthUserFuncExample.Criteria criteria = authUserFuncExample.createCriteria();
        //用户编号
        criteria.andBoIdEqualTo(authFuncBo.getBoId());
        //业务类型
        criteria.andBusTypeEqualTo(authFuncBo.getBusType());
        //所属系统
        criteria.andSystemIdEqualTo(authFuncBo.getSystemId());
        //子系统编号
        criteria.andSubSystemIdEqualTo(authFuncBo.getSubSystemId());
        //可用
        criteria.andIsEnabledEqualTo(CommonDsConstant.IS_ENABLED_VAL);
        List<AuthUserFunc> userFuncBoList = authUserFuncMapper.selectByExample(authUserFuncExample);
        //判断结果是否为空
        if (CollectionUtils.isEmpty(userFuncBoList)) {
            log.warn("查询结果为空！");
            return null;
        }
        //功能编号集合
        List<Long> funcIdList = new ArrayList<Long>();
        for (AuthUserFunc authUserFunc : userFuncBoList) {
            funcIdList.add(authUserFunc.getFuncId());
        }
        authFuncBo.setFuncIdList(funcIdList);
        authFuncBoList = selSubList(authFuncBo);
        return authFuncBoList;
    }

    /**
     * 递归查询子孙功能集合
     *
     * @param authFuncBo
     * @return java.util.List<com.nxin.middle.basic.auth.domain.AuthFuncBo>
     * @author
     * @since 2019/4/22 15:38
     */
    @Override
    public List<AuthFuncBo> selSubList(AuthFuncBo authFuncBo) {
        List<AuthFunc> authFuncList = selFuncListByParam(authFuncBo);
        if (CollectionUtils.isEmpty(authFuncList)) {
            log.info("根据条件查询子功能集合结果为空！");
            return null;
        }
        List<AuthFuncBo> authFuncBoList = new ArrayList<AuthFuncBo>();
        AuthFuncBo entity = null;
        for (AuthFunc func : authFuncList) {
            entity = new AuthFuncBo();
            BeanCopyDsUtil.copyProperties(func, entity);
            //复制属性
            AuthFuncBo param = BeanCopierDsUtil.copyProperties(authFuncBo, AuthFuncBo.class);
            //父级功能编号
            param.setParentFuncId(func.getId());
            //子功能集合
            entity.setSubFuncList(selSubList(param));
            authFuncBoList.add(entity);
        }
        return authFuncBoList;
    }

    /**
     * 查询用户功能权限
     *
     * @param authFuncBo
     * @return java.util.List<com.nxin.middle.basic.auth.domain.AuthUserFuncBo>
     * @author
     * @since 2019/4/22 13:47
     */
    @Override
    public List<AuthUserFuncBo> selAuthFuncByParam(AuthFuncBo authFuncBo) {
//        if (authFuncBo == null || authFuncBo.getSystemId() == null || Strings.isBlank(authFuncBo.getCode())
//                || Strings.isBlank(authFuncBo.getUserTypeLike()) || authFuncBo.getSubSystemId() == null) {
//            throw new ValidationException(Errors.SYSTEM_REQUEST_PARAM_ERROR);
//        }
        String code = authFuncBo.getCode();
        String[] split = code.split(",");
        List<String> codeList = new ArrayList<String>();
        for (String s : split) {
            codeList.add(s);
        }
        authFuncBo.setCodeList(codeList);
        log.info("查询用户功能权限请求入参为：{}", authFuncBo.toString());
        List<AuthFunc> authFuncList = selFuncListByParam(authFuncBo);
        if (CollectionUtils.isEmpty(authFuncList)) {
            log.info("根据条件查询子功能集合结果为空！");
            return null;
        }
        //功能id集合
        List<Long> funcIdList = new ArrayList<Long>();
        for (AuthFunc func : authFuncList) {
            funcIdList.add(func.getId());
        }
        log.info("功能id集合个数为：{}", funcIdList.size());
        //根据条件查询用户有无此功能权限
        AuthUserFuncExample authUserFuncExample = new AuthUserFuncExample();
        AuthUserFuncExample.Criteria criteria = authUserFuncExample.createCriteria();
        //用户编号
        criteria.andBoIdEqualTo(authFuncBo.getBoId());
        //功能编号集合
        criteria.andFuncIdIn(funcIdList);
        //业务编号
        criteria.andBusIdEqualTo(authFuncBo.getBusId());
        //业务类型
        criteria.andBusTypeEqualTo(authFuncBo.getBusType());
        //子系统编号
        criteria.andSubSystemIdEqualTo(authFuncBo.getSubSystemId());
        //所属系统
        criteria.andSystemIdEqualTo(authFuncBo.getSystemId());
        //可用
        criteria.andIsEnabledEqualTo(CommonDsConstant.IS_ENABLED_VAL);
        List<AuthUserFunc> authUserFuncList = authUserFuncMapper.selectByExample(authUserFuncExample);
        if (CollectionUtils.isEmpty(authUserFuncList)) {
            return null;
        }
        log.info("查询用户有无此功能权限结果为:{}", authUserFuncList.size());
        List<AuthUserFuncBo> authUserFuncBoList = BeanCopierDsUtil.copyPropertiesOfList(authUserFuncList, AuthUserFuncBo.class);
        return authUserFuncBoList;
    }

//    selSubList

    /**
     * 后台角色管理查询所有权限
     *
     * @param funcBo
     * @return java.util.List<com.nxin.middle.basic.auth.domain.AuthFuncBo>
     * @author
     * @date 15:15 2019/6/25
     */
    @Override
    public List<AuthFuncBo> selHtAndOrtherFuncList(AuthFuncBo funcBo) {
//        if (funcBo == null || funcBo.getSystemId() == null) {
//            throw new ValidationException(Errors.SYSTEM_REQUEST_PARAM_ERROR);
//        }
        if (funcBo.getParentFuncId() == null) {
            funcBo.setParentFuncId(0L);
        }
        // isback = 1
        funcBo.setIsBack(CommonDsConstant.IS_ENABLED_VAL);
        List<AuthFuncBo> authFuncBoList = selSubList(funcBo);
        return authFuncBoList;
    }

    /**
     * 根据code查询用户是否有该模块权限
     *
     * @param funcBo
     * @return java.util.List<com.nxin.middle.basic.auth.domain.AuthFuncBo>
     * @author
     * @date 15:15 2019/6/25
     */
    @Override
    public Boolean judgeUserFuncByCode(AuthFuncBo funcBo) {
//        if (funcBo == null || funcBo.getSystemId() == null || Strings.isBlank(funcBo.getCode()) || funcBo.getSubSystemId() == null) {
//            throw new ValidationException(Errors.SYSTEM_REQUEST_PARAM_ERROR);
//        }
        if (funcBo.getBoId().equals(funcBo.getAuthBoId())) {
            return true;
        }
        String code = funcBo.getCode();
        String[] split = code.split(",");
        List<String> codeList = new ArrayList<String>();
        for (String s : split) {
            codeList.add(s);
        }
        funcBo.setCodeList(codeList);
        log.info("查询用户功能权限请求入参为：{}", funcBo.toString());
        List<AuthFunc> authFuncList = selFuncListByParam(funcBo);
        if (CollectionUtils.isEmpty(authFuncList)) {
            log.info("根据条件查询子功能集合结果为空！");
            return false;
        }
        //功能id集合
        List<Long> funcIdList = new ArrayList<Long>();
        for (AuthFunc func : authFuncList) {
            funcIdList.add(func.getId());
        }
        log.info("功能id集合个数为：{}", funcIdList.size());
        //根据条件查询用户有无此功能权限
        AuthUserFuncBo userFuncBo = new AuthUserFuncBo();
        userFuncBo.setBoId(funcBo.getBoId());
        userFuncBo.setAuthBoId(funcBo.getAuthBoId());
        userFuncBo.setSystemId(funcBo.getSystemId());
        userFuncBo.setFuncIdList(funcIdList);
        List<AuthUserFunc> authUserFuncs = authUserFuncService.selUserFuncListByParam(userFuncBo);
        log.info("查询到用户功能集合{}", authUserFuncs);
        if (CollectionUtils.isNotEmpty(authUserFuncs)) {
            return true;
        }
        return false;
    }

    /**
     * 特殊模式下查询功能列表，主要用于权限管理非本子系统下的功能
     *
     * @param funcBo
     * @return java.util.List<com.nxin.middle.basic.auth.domain.AuthFuncBo>
     * @author
     * @date 17:55 2020/4/18
     */
    @Override
    public List<AuthFuncBo> selSpecAuthFuncList(AuthSpecFuncBo funcBo) {
        List<AuthFuncBo> funcBoList = new ArrayList<>();
        //查询父级节点功能
        AuthFuncExample funcExp = new AuthFuncExample();
        AuthFuncExample.Criteria funcExpCri = funcExp.createCriteria();
        funcExpCri.andCodeEqualTo(funcBo.getParentFuncCode());
        funcExpCri.andSystemIdEqualTo(funcBo.getSystemId());
        funcExpCri.andIsEnabledEqualTo(CommonDsConstant.IS_ENABLED_VAL);
        List<AuthFunc> funcList = authFuncMapper.selectByExample(funcExp);
        if (CollectionUtils.isEmpty(funcList)) {
            return funcBoList;
        }
        //查询父级节点下功能
        AuthFunc authFunc = funcList.get(0);
        funcExp = new AuthFuncExample();
        funcExpCri = funcExp.createCriteria();
        funcExpCri.andParentFuncIdEqualTo(authFunc.getId());
        funcExpCri.andIsEnabledEqualTo(CommonDsConstant.IS_ENABLED_VAL);
        funcExpCri.andSystemIdEqualTo(funcBo.getSystemId());
        funcExpCri.andSubSystemIdEqualTo(funcBo.getSubSystemId());
        funcExpCri.andTypeIn(funcBo.getTypeList());
        funcList = authFuncMapper.selectByExample(funcExp);
        if (CollectionUtils.isEmpty(funcList)) {
            return funcBoList;
        }
        Map<Long, AuthFunc> funcMap = funcList.stream().collect(Collectors.toMap(AuthFunc::getId, a1 -> a1, (k1, k2) -> k1));
        //查询用户所有的角色
        AuthRoleMemberExample roleMemberExp = new AuthRoleMemberExample();
        AuthRoleMemberExample.Criteria roleMemberCri = roleMemberExp.createCriteria();
        roleMemberCri.andBoIdEqualTo(funcBo.getBoId());
        roleMemberCri.andSystemIdEqualTo(funcBo.getSystemId());
        roleMemberCri.andIsEnabledEqualTo(CommonDsConstant.IS_ENABLED_VAL);
        List<AuthRoleMember> roleMemberList = authRoleMemberMapper.selectByExample(roleMemberExp);
        if (CollectionUtils.isEmpty(roleMemberList)) {
            return funcBoList;
        }
        //查询角色功能对应的关系信息
        List<String> roleCodeList = roleMemberList.stream().map(AuthRoleMember::getRoleCode).collect(Collectors.toList());
        AuthRoleFuncExample roleFuncExp = new AuthRoleFuncExample();
        AuthRoleFuncExample.Criteria criteria = roleFuncExp.createCriteria();
        criteria.andRoleCodeIn(roleCodeList);
        criteria.andSystemIdEqualTo(funcBo.getSystemId());
        criteria.andSubSystemIdEqualTo(funcBo.getSubSystemId());
        criteria.andIsEnabledEqualTo(CommonDsConstant.IS_ENABLED_VAL);
        List<AuthRoleFunc> roleFuncList = authRoleFuncMapper.selectByExample(roleFuncExp);
        if (CollectionUtils.isEmpty(roleFuncList)) {
            return funcBoList;
        }
        for (AuthRoleFunc roleFunc : roleFuncList) {
            authFunc = funcMap.get(roleFunc.getFuncId());
            if (authFunc != null) {
                funcBoList.add(BeanCopierDsUtil.copyProperties(authFunc, AuthFuncBo.class));
            }
        }
        return funcBoList;
    }


    @Override
    public List<AuthFuncBo> selectAuthAll(AuthFuncBo authFuncBo) {
        List<AuthFunc> authFuncList = authFuncExMapper.selectAuthAll(authFuncBo);
        List<AuthFuncBo> funcBoList = BeanCopierDsUtil.copyPropertiesOfList(authFuncList, AuthFuncBo.class);
        //遍历
        for (AuthFuncBo funcBo : funcBoList) {
            //获取父id
            Long parentFuncId = funcBo.getParentFuncId();
            if (parentFuncId != null) {
                AuthFunc authFunc = authFuncMapper.selectByPrimaryKey(parentFuncId);
                funcBo.setParentFuncName(authFunc.getName());
            } else {
                funcBo.setParentFuncName("无");
            }

        }
        return funcBoList;
    }

    @Override
    public PageInfo selectAuthAllByPage(AuthFuncBo bo) {
        Integer pageNum = CommonDsConstant.PAGE_NUM;
        if (bo.getPageNum() != null) {
            pageNum = bo.getPageNum();
        }
        Integer pageSize = CommonDsConstant.PAGE_SIZE;
        if (bo.getPageSize() != null) {
            pageSize = bo.getPageSize();
        }
        //设置分页
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo(this.selectAuthAll(bo));
    }
}
